package com.kaitian.demo.cloud.eureka_client1.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import feign.Param;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：xietian
 * @version ：V1.0
 * @program ：demo
 * @date ：Created in 2022/4/13 14:37
 * @description ：
 */
@RestController("/test")
public class TestController {

    @HystrixCommand(
        commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "3000")
        }
    )
    @LoadBalanced
    @PostMapping("/printStr")
    public String printStr(@Param("str") String str) throws Exception {
        Long sleep = RandomUtils.nextInt(10000) * 1L;
        Thread.sleep(sleep);
        if (sleep % 3 == 0) {
            throw new Exception();
        }
        System.out.println(str);
        return str;
    }

    public String error() {
        return "hystrix  error";
    }
}